package com.lanou.projecta_163news.fragment.mainAdapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.projecta_163news.R;

import java.util.List;

import static com.lanou.projecta_163news.R.id.read_item_content_tv;

/**
 * Created by dllo on 16/3/9.
 */
public class ReadRecyclerAdapter extends RecyclerView.Adapter<ReadRecyclerAdapter.ReRvViewHolder> {
    private List<Read> reData;
    private int position;
    private ReadItemListener readItemListener;

    public void setReRvItemListener(ReadItemListener readItemListener) {
        this.readItemListener = readItemListener;
    }

    public void addReData(List<Read> reData) {
        this.reData = reData;
        notifyDataSetChanged();
    }

    @Override
    public ReRvViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.read_rv_item,null);
        return new ReRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ReRvViewHolder holder, int position) {
        if (reData != null && reData.size() >0){
            Read read = reData.get(position);
            holder.readIv.setImageResource(read.getImageIv());
            holder.readConTv.setText(read.getContentTv());
            holder.readNameTv.setText(read.getNameTv());
            holder.position = position;
        }

    }

    @Override
    public int getItemCount() {
        return reData != null && reData.size() > 0 ? reData.size() : 0;
    }

    public class ReRvViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView readIv;
        private TextView readConTv, readNameTv;
        private LinearLayout readLine;
        private int position;

        public ReRvViewHolder(View itemView) {
            super(itemView);
            readIv = (ImageView) itemView.findViewById(R.id.read_item_iv);
            readConTv = (TextView) itemView.findViewById(read_item_content_tv);
            readNameTv = (TextView) itemView.findViewById(R.id.read_item_name_tv);
            readLine = (LinearLayout) itemView.findViewById(R.id.read_line);
            readLine.setOnClickListener(this);


        }

        @Override
        public void onClick(View v) {
            Read r = reData.get(position);
            if (readItemListener != null) {
                readItemListener.reRvItemListener(r);
            }

        }
    }
}
